package edu.ssm.campusAssistant.service.impl;

import edu.ssm.campusAssistant.bo.AddDemoListReq;
import edu.ssm.campusAssistant.bo.DemoBO;
import edu.ssm.campusAssistant.bo.rsp.RspBooleanBO;
import edu.ssm.campusAssistant.constant.RspConstants;
import edu.ssm.campusAssistant.dao.DemoDao;
import edu.ssm.campusAssistant.exception.BusinessException;
import edu.ssm.campusAssistant.exception.ParametersException;
import edu.ssm.campusAssistant.po.DemoPO;
import edu.ssm.campusAssistant.service.AddDemoListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 标题:批量新增例子服务实现
 * 描述:
 *
 * @autho LXF
 * @date 2019/4/8 17:49
 */
@Service
public class AddDemoListServiceImpl implements AddDemoListService {
    private static final Logger logger = LoggerFactory.getLogger(AddDemoListServiceImpl.class);
    private final boolean isDebugEnabled = logger.isDebugEnabled();
    @Autowired
    private DemoDao demoDao;

    @Override
    public RspBooleanBO addDemoList(AddDemoListReq addDemoListReq) {
        if (isDebugEnabled) {
            logger.debug("批量新增例子服务入参{}" + addDemoListReq.toString());
        }
        // 参数非空校验
        checkParameters(addDemoListReq);
        try {
            //转换List<DemoPO>
            List<DemoPO> demoPOS = TransferDemoPO(addDemoListReq);
            //批量插入
            int result = demoDao.insertBatchDemo(demoPOS);
            if (result < 1) {
                return new RspBooleanBO(false, "批量新增失败");
            } else {
                return new RspBooleanBO(true, "批量新增成功");
            }
        } catch (Exception e) {
            logger.error("批量新增例子服务失败", e);
            throw new BusinessException(RspConstants.RSP_CODE_BUSI_SERVICE_ERROR, "批量新增例子服务失败");
        }
    }

    /**
     * 转换List<DemoPO>
     *
     * @param addDemoListReq 服务入参
     * @return List<DemoPO>
     */
    private List<DemoPO> TransferDemoPO(AddDemoListReq addDemoListReq) {
        if (addDemoListReq.getCreateDate() == null) {
            addDemoListReq.setCreateDate(new Date());
        }
        List<DemoPO> demoPOS = new ArrayList<DemoPO>();
        for (DemoBO demoBO : addDemoListReq.getDemoBOList()) {
            DemoPO demoPO = new DemoPO();
            demoPO.setUserName(demoBO.getUserName());
            demoPO.setPassword(demoBO.getPassword());
            demoPO.setCreateDate(addDemoListReq.getCreateDate());
            demoPOS.add(demoPO);
        }
        return demoPOS;
    }

    /**
     * 参数校验
     *
     * @param addDemoListReq 服务入参
     */
    private void checkParameters(AddDemoListReq addDemoListReq) {
        if (null == addDemoListReq.getDemoBOList() || addDemoListReq.getDemoBOList().size() < 1) {
            throw new ParametersException("批量新增例子服务入参[demoBOList]不能为空");
        }
        for (DemoBO demoBO : addDemoListReq.getDemoBOList()) {
            if (StringUtils.isEmpty(demoBO.getUserName())) {
                throw new ParametersException("批量新增例子服务入参[userName]不能为空");
            }
            if (StringUtils.isEmpty(demoBO.getPassword())) {
                throw new ParametersException("批量新增例子服务入参[password]不能为空");
            }
        }
    }

}
